﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Telerik.Windows.Controls;

namespace LightSwitchApplication.UserControls.ReportChart
{
    public class SalesViewModel : ViewModelBase
    {
        private bool _chartAnimationsEnabled = true;
        ObservableCollection<IEnumerable<Sales>> _data;
        private IEnumerable<Sales> _monthData;
        private IEnumerable<Sales> _pieData;
        private Random _rand = new Random();
        private IEnumerable<Sales> _yearData;
        private string[] months = new string[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
        private string[] products = new string[] { "Mercedes", "Audi", "BMW", "Mitsubishi", "Toyota", "Honda" };
        private string[] weeks = new string[] { "Week 1", "Week 2", "Week 3", "Week 4" };

        public bool ChartAnimationsEnabled
        {
            get
            {
                return this._chartAnimationsEnabled;
            }
            private set
            {
                if (this._chartAnimationsEnabled == value)
                    return;

                _chartAnimationsEnabled = value;
                this.OnPropertyChanged("ChartAnimationsEnabled");
            }
        }

        public ObservableCollection<IEnumerable<Sales>> Data
        {
            get
            {
                if (this._data == null)
                {
                    this._data = new ObservableCollection<IEnumerable<Sales>>();
                    this._data.Add(this.YearlyData);
                    this._data.Add(this.MonthData);
                    this._data.Add(this.PieData);
                }

                return this._data;
            }
        }

        public IEnumerable<Sales> MonthData
        {
            get
            {
                if (this._monthData == null)
                {
                    List<Sales> sales = new List<Sales>();

                    foreach (string month in this.months)
                    {
                        sales.Add(new Sales(_rand.Next(30, 100), month));
                    }

                    this._monthData = sales;
                }

                return this._monthData;
            }
        }

        public IEnumerable<Sales> PieData
        {
            get
            {
                if (this._pieData == null)
                {
                    List<Sales> sales = new List<Sales>();

                    foreach (string product in this.products)
                    {
                        sales.Add(new Sales(_rand.Next(30, 100), product));
                    }

                    this._pieData = sales;
                }

                return this._pieData;
            }
        }

        public IEnumerable<Sales> WeeklyData
        {
            get
            {
                List<Sales> sales = new List<Sales>();

                foreach (string week in this.weeks)
                {
                    sales.Add(new Sales(_rand.Next(30, 100), week));
                }

                return sales;
            }
        }

        public IEnumerable<Sales> YearlyData
        {
            get
            {
                if (this._yearData == null)
                {
                    List<Sales> sales = new List<Sales>();

                    for (int i = 2000; i < 2010; i++)
                    {
                        sales.Add(new Sales(_rand.Next(29, 91), i.ToString()));
                    }

                    this._yearData = sales;
                }

                return this._yearData;
            }
        }

        public void ChangePieData()
        {
            this.ChartAnimationsEnabled = false;
            this._data.Remove(this._pieData);
            this._pieData = null;
            this._data.Add(this.PieData);
            this.OnPropertyChanged("PieData");
        }
    }
}
